import Mock from 'mockjs'
import { useUserListStore, useUserStore } from '@/stores/user'

Mock.mock('/user/changePasswoed', 'post', (config) => {
    const req = JSON.parse(config.body)
    const userList = useUserListStore()
    const user = useUserStore()
    let flag = false
    const data = {
        code: '200',
        message: '原密码错误',
        data: {}
    }
    userList.userList.forEach(item => {
        if (item.userid == req.userid && item.password == req.oldPassword) {
            item.password = req.password
            flag = true
            data.message = '修改成功，请重新登录'
            data.data = {
                user: item
            }
            user.clear()
        }
    })

    return data
})

Mock.mock('/user/changeInfo', 'post', (config) => {
    const req = JSON.parse(config.body)
    const userList = useUserListStore()
    const user = useUserStore()
    userList.changeInfo(req)
    user.changeUserInfo(userList.getUserInfo(req.userid))
    return {
        code: '200',
        message: '修改成功',
        data: {
            user: user.userInfo
        }
    }
})

Mock.mock('/user/concern', 'post', (config) => {
    const req = JSON.parse(config.body)
    const user = useUserStore()
    const userList = useUserListStore()
    const data = {
        code: '200',
        message: '你已经关注了'
    }

    if (req.userid == req.concernUserid) {
        data.message = '你不能关注自己'
    } else {
        const flag = userList.addConcern(req)
        if (flag) {
            data.message = '关注成功'
            user.changeUserInfo(userList.getUserInfo(req.userid))
        }
    }
    
    return data
})